package com.sfh.controller.task;

import com.jfinal.plugin.activerecord.Db;
import com.sfh.base.Task;
import com.sfh.service.model.UserModel;
import com.sfh.service.model.table.UserTable;
import com.sfh.service.util.SQLHelp;

/***
 * 处理用户 魅力值/财力值
 * 
 * @author DELL
 *
 */
public class TaskUserWealthCharm extends Task {

	long charm;
	long wealth;
	long userId;

	public TaskUserWealthCharm(long userId, long charm, long wealth) {
		this.userId = userId;
		this.charm = charm;
		this.wealth = wealth;
	}

	public boolean run() {
		System.out.println("处理用户:" + userId + "  魅力值:" + charm + "财力值:" + wealth);
		UserModel userModel = UserModel.USER.findById(userId);
		if (userModel == null) {
			return false;
		}
		final long version = userModel.getVerion();
		
		SQLHelp sql = new SQLHelp(UserTable.Table);
		sql.addWhereEqualTo(UserTable.id, userId);
		sql.addWhereEqualTo(UserTable.version, version);

		sql.updateKeys(UserTable.version, version + 1);
		sql.updateKeys(UserTable.charm, userModel.getCharm() + charm);
		sql.updateKeys(UserTable.wealth, userModel.getWealth() + wealth);

		int index = Db.update(sql.buildUpdaete());
		return index > 0;
	}
}
